import {Component, EventEmitter, OnInit, Input, Output, OnChanges} from '@angular/core';
import {IndexService} from "../../services/index.service";
import {TipService} from "../../services/tip.service";


@Component({
  selector: 'app-shop-cart-list',
  templateUrl: './shop-cart-list.component.html',
  styleUrls: ['./shop-cart-list.component.css']
})

export class ShopCartListComponent implements OnInit, OnChanges {

  // 购物车列表
  shoppingList = [];

  // 商家名称
  mname = '';

  @Input() shoppingShow: boolean;
  @Output() shopList = new EventEmitter();

  constructor(private tipService: TipService,
              private indexService: IndexService) {
  }


  ngOnInit() {
    let userInfo = sessionStorage.getItem('userInfo');
    this.mname = userInfo['mname'];
  }

  ngOnChanges() {
    if (this.shoppingShow == true) {
      this.shoppingList = this.indexService.getData();
    }
  }

  // 加菜
  add(parent, item, inx) {
    if (item.type == '0') {
      parent.count++;
      item.count++;
      this.indexService.updateData(this.shoppingList);
    } else if (item.type == '1') {
      item.typeList.radioType[inx]['count']++;
      parent.count++;
      item.count++;
      this.indexService.updateData(this.shoppingList);
    } else if (item.type == '2') {
      item.typeList.multiType[inx]['count']++;
      parent.count++;
      item.count++;
      this.indexService.updateData(this.shoppingList);
    }
    else if (item.type == '3') {
      item.typeList.pumpType[inx]['count']++;
      parent.count++;
      item.count++;
      this.indexService.updateData(this.shoppingList);
    }

  }

  //减菜
  reduce(parent, item, inx) {
    if (item.type == '0') {
      if (item.count > 0) {
        parent.count--;
        item.count--;
        this.indexService.updateData(this.shoppingList);
      }
    } else if (item.type == '1') {

      if (item.typeList.radioType[inx]['count'] > 0) {
        item.typeList.radioType[inx]['count']--;
        if (item.typeList.radioType[inx]['count'] == 0) {
          item.typeList.radioType.splice(inx,1);
        }
        parent.count--;
        item.count--;
        this.indexService.updateData(this.shoppingList);
      }

    } else if (item.type == '2') {
      if (item.typeList.multiType[inx]['count'] > 0) {
        item.typeList.multiType[inx]['count']--;
        if (item.typeList.multiType[inx]['count'] == 0) {
          item.typeList.multiType.splice(inx,1);
        }
        parent.count--;
        item.count--;
        this.indexService.updateData(this.shoppingList);
      }
    } else if (item.type == '3') {
      if (item.typeList.pumpType[inx]['count'] > 0) {
        item.typeList.pumpType[inx]['count']--;
        if (item.typeList.pumpType[inx]['count'] == 0) {
          item.typeList.pumpType.splice(inx,1);
        }
        parent.count--;
        item.count--;
        this.indexService.updateData(this.shoppingList);
      }
    }

    // 删除菜品以后，购物车列表空时，关闭列表
    let selectCount = 0;
    this.shoppingList.forEach(v => {
      selectCount += v.count;
    });

    if (!selectCount) {
      this.shopList.emit(false);
    }

    // console.log(item)

  }

  // 清空购物车
  clearShopping() {
    this.indexService.clearData();
    this.shopList.emit(false);
  }

}
